; execfun.asm
; event handler for controls

	.include "common.inc"
	.include "global.inc"
	.include "window.inc"
	.include "cursor.inc"
	.include "drawfun.inc"
	.include "util.inc"
	.include "screen.inc"
	
	.export ex_screen
	.export ex_window
	.export ex_button
	.export ex_edit
	.export ex_titlebar
	
	.import edrawbeam
	.import dodraw
	
	.segment "DATA"
	
eftyp:		.byte $00
	
	.segment "CODE"
	
ex_screen:	ldx #CURSOR_CROSS
			jmp commonex

ex_window:	ldx #CURSOR_ARROW
			jsr commonex
			jmp parevent

ex_button:	ldx #CURSOR_HAND
exbutton1:	jsr commonex
			bcs @chkenter
			beq @nomouse
@noenter:	lda eftyp
			cmp #EVT_MOUSE_LEAVE
			bne @noleave
			jsr win_getstate
			cmp #BSTATE_DOWN
			bne @endexb
			beq @mup
@noleave:	cmp #EVT_MOUSE_DOWN
			bne @nodn
			; down
@btndn:		lda #BSTATE_DOWN
			bne	@bsetst
@nodn:		cmp #EVT_MOUSE_UP
			bne @endexb
			; up
@mup:		lda #BSTATE_UP
@bsetst:	jmp upd_btnst
@chkenter:	lda eftyp
			cmp #EVT_MOUSE_ENTER
			bne @endexb
			ldy #MouseEvent::btn
			lda (reg2), y
			bne @btndn
@endexb:	rts
@nomouse:	jmp parevent

ex_edit:	ldx #CURSOR_BEAM
			jsr commonex
			bne @done
			lda #KEY_EVENT
			bit eftyp
			bne @edo_keys
			lda eftyp
			cmp #EVT_WIN_SETFOCUS
			bne @chkkill
			jsr win_getstate			; set focus
			eor #EDIT_HAS_FOCUS
			bpl @done
			jsr win_setstate
			jmp @updbeam
@chkkill:	cmp #EVT_WIN_KILLFOCUS
			bne @done
			jsr win_getstate			; kill focus
			and #~EDIT_HAS_FOCUS
			jsr win_setstate
@updbeam:	jmp edrawbeam
@done:		rts
@edo_keys:	; handle keys
			lda eftyp
			cmp #EVT_KEY_DOWN
			bne @done
			ldy #KeyEvent::petscii
			lda (reg2), y
			bpl @chkascii
			cmp #$A0
			bcc @ectrl
			bcs @eprint
@chkascii:	cmp #$20
			bcc @ectrl
@eprint:	ldy #KeyEvent::scrcode
			lda (reg2), y
			jmp eputch
@ectrl:		cmp #$13	; HOME
			beq @dohome
			cmp #$14	; DEL
			beq @dodel
			cmp #$93	; CLR
			beq @doclr
			cmp #$94	; INS
			beq @doins
			cmp #$1D	; CRSR R
			beq @docrsrr
			cmp #$9D	; CRSR L
			beq @docrsrl
			jmp escalate
			
@dohome:	jmp ehome
@doclr:		jmp eclr
@dodel:		jmp edel
@doins:		jmp eins
@docrsrr:	jmp ecrsrr
@docrsrl:	jmp ecrsrl

ex_titlebar:
			jsr win_getparent
			beq @done_ext
			jsr win_isfront
			bcs ex_movebar
			ldx #CURSOR_ACTIVATE
			jsr exbutton1
			lda eftyp
			cmp #EVT_MOUSE_UP
			bne @done_ext
			jsr win_getparent
			jsr win_show
@done_ext:	rts

ex_movebar:	ldx #CURSOR_MOVE
			jsr exbutton1
			lda eftyp
			cmp #EVT_MOUSE_DOWN
			beq @begmove
			cmp #EVT_MOUSE_UP
			beq @endmove
			cmp #EVT_MOUSE_MOVE
			beq @domove
			rts
			
@begmove:	inc inmove
			ldy #MouseEvent::mx
			lda (reg2), y
			sta moveorgx
			ldy #MouseEvent::my
			lda (reg2), y
			sta moveorgy
			jmp set_capture

@endmove:	dec inmove
			jmp rel_capture

@notinmove:	rts

@domove:	lda inmove
			beq @notinmove
			lda #0
			sta ismove
			ldy #MouseEvent::mx
			lda (reg2), y
			sec
			sbc moveorgx
			sta movedx
			tax
			bpl @xpos
			eor #$FF
			tax
			inx
@xpos:		cpx #8
			bcc @nomovex
			inc ismove
@nomovex:	ldy #MouseEvent::my
			lda (reg2), y
			sec
			sbc moveorgy
			sta movedy
			tay
			bpl @ypos
			eor #$FF
			tay
			iny
@ypos:		cpy #16
			bcc @nomovey
			inc ismove
@nomovey:	lda ismove
			beq @donemv

			lda movedx
			and #$F8
			sta movedx
			clc
			adc moveorgx
			sta moveorgx
			lda movedy
			and #$F0
			sta movedy
			clc
			adc moveorgy
			sta moveorgy

			jsr win_getfrm
			sta mfrm
			jsr frm_getpos
			txa
			clc
			adc movedx
			cmp scr_dim_x
			bcs @nochngx
			tax
@nochngx:	lda movedy
			bpl @noovfl
			tya
			beq @nochngy
@noovfl:	tya
			clc
			adc movedy
			cmp scr_dim_y
			bcs @nochngy
			tay
@nochngy:	lda mfrm
			jsr frm_setpos
			jsr cur_hide
			jsr redraw_screen
			jsr cur_show
@donemv:	rts

	.segment "DATA"

inmove:		.byte 0
mfrm:		.byte 0	
ismove:		.byte 0
moveorgx:	.byte 0
moveorgy:	.byte 0
movedx:		.byte 0
movedy:		.byte 0
ytmp:		.byte 0
	
	.segment "CODE"

commonex:	ldy #Event::type
			lda (reg2), y
			sta eftyp
			and #MOUSE_EVENT
			beq @nothandled
			; mouse move -> change cursor
			lda eftyp
			cmp #EVT_MOUSE_ENTER
			bne @nothandled
			txa
			pha
			jsr win_isactive
			pla
			bcs @setcur
			lda #CURSOR_WINDOW
@setcur:	jsr cur_set
			sec
			bcs @endex
@nothandled:
			clc
@endex:		lda eftyp
			and #MOUSE_EVENT
			rts

parevent:	lda #KEY_EVENT
			bit eftyp
			bne @doesc
			rts
@doesc:		jmp escalate

	.segment "DATA"
	
esccode:	.byte $00
emaxch:		.byte $00
estate:		.byte $00
epos:		.byte $00
	
	.segment "CODE"

seltxt:		sta esccode
			jsr win_gettext
			sta reg2
			stx reg2+1
			ldy #0
			lda (reg2), y
			sta emaxch
			jsr win_getstate
			tax
			and #~EDIT_POS_MASK
			sta estate
			txa
			and #EDIT_POS_MASK
			rts

eputch:		sta esccode
			jsr seltxt
			cmp emaxch
			bcs invinput
			tay
			iny
			lda esccode
			sta (reg2), y
showpos:	tya
			ora estate
			jsr win_setstate
showpos1:	jsr cur_hide
			jsr dodraw
			jmp cur_show
			
invinput:	rts		; TODO: beep

edel:		jsr seltxt
			beq invinput
			tay
			sta epos
			ldy emaxch
			ldx #$20	; space
@mvloop:	lda (reg2), y
			pha
			txa
			sta (reg2), y
			pla
			tax
			dey
			cpy epos
			bcs @mvloop
			jmp showpos

eins:		jsr seltxt
			cmp emaxch
			bcs invinput
			tay
			ldx #$20	; space
@mvloop:	iny
			lda (reg2), y
			pha
			txa
			sta (reg2), y
			pla
			tax
			cpy emaxch
			bcc @mvloop
			jmp showpos1

ecrsrl:		jsr seltxt
			beq invinput
			tay
			dey
			jmp showpos
			
ecrsrr:		jsr seltxt
			cmp emaxch
			bcs invinput
			tay
			iny
			jmp showpos

ehome:		jsr seltxt
			beq invinput
			ldy #0
			jmp showpos

eclr:		jsr seltxt
			ldy emaxch
			lda #$20	; space
@clrloop:	sta (reg2), y
			dey
			bne @clrloop
			jmp showpos
